package com.leyou.search.web;

import com.leyou.common.exceptions.LyException;
import com.leyou.item.client.ItemClient;
import com.leyou.item.dto.CategoryDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.SpringTemplateEngine;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*@Slf4j
//@RestController
public class PageService_bak {


    @Autowired
    private ItemClient itemClient;

    //@Autowired
    private SpringTemplateEngine templateEngine;

    @Value("${ly.static.itemDir}")
    private String itemDir;
    @Value("${ly.static.itemTemplate}")
    private String itemTemplate;

    @GetMapping("haha")
    public Map<String, Object> loadItemData(Long id) {

        //获取所有的父节点信息
        List<CategoryDTO> parentList = itemClient.queryCategoryOfParent(id);
        //查询二级的字节点

        for (CategoryDTO categoryDTO : parentList) {
            List<CategoryDTO> categoryDTOS = itemClient.queryCategoryOfParent(categoryDTO.getId());
            categoryDTO.setListCategoryDTO(categoryDTOS);
            //查询父节点子类的子类
            for (CategoryDTO dto : categoryDTOS) {
                List<CategoryDTO> dtoList = itemClient.queryCategoryOfParent(dto.getId());
                dto.setListCategoryDTO(dtoList);
            }
        }



        HashMap<String, Object> map = new HashMap<>();

        map.put("parentList",parentList);



        return map;

    }

    public void createItemHtml(Long id) {
        // 上下文，准备模型数据
        Context context = new Context();
        // 调用之前写好的加载数据方法
        context.setVariables(loadItemData(id));
        // 准备文件路径
        File dir = new File(itemDir);
        if (!dir.exists()) {
            if (!dir.mkdirs()) {
                // 创建失败，抛出异常
                log.error("【静态页服务】创建静态页目录失败，目录地址：{}", dir.getAbsolutePath());
                throw new LyException(400,"失败");
            }
        }
        File filePath = new File(dir,  "index.html");
        // 准备输出流
        try (PrintWriter writer = new PrintWriter(filePath, "UTF-8")) {
            templateEngine.process(itemTemplate, context, writer);
        } catch (IOException e) {
            log.error("【静态页服务】静态页生成失败，商品id：{}", id, e);
            throw new LyException(400,"哈哈失败");
        }
    }*//*

}*/
